let BASE_URL = "https://www.manshetianji.cn/";
const request = (obj, data, loading) => {
	if (loading) uni.showLoading({
		title: "加载中",
		mask: true
	});
	let Authorization = uni.getStorageSync("token") || ""
	let httpDefaultOpts = {
		url: BASE_URL + obj.url,
		data: data,
		beforeSend: function(xmlHttp) {
			xmlHttp.setRequestHeader("If-Modified-Since", "0");
			xmlHttp.setRequestHeader("Cache-Control", "no-cache");
		},
		method: obj.method || 'post',
		dataType: "json",
		header: {
			"Content-type": "application/json",
			"lang": "zh",
			"group-id": "2",
		},
	};
	// console.log("Authorization",Authorization)
	if (Authorization) {
		if (httpDefaultOpts.header) {
			// console.log("1111")
			httpDefaultOpts.header.token = Authorization
		} else {
			console.log("2222")
			httpDefaultOpts.header["token"] = Authorization;
		}
	} else {
		// console.log("获取不到userToken");
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts)
			.then((res) => {
				// console.log("res,res", res)
				if (loading) uni.hideLoading();
				if (res.statusCode == 200) {
					resolve(res.data);
				} else if (res.data.code == 401) {
					// uni.showToast({
					// 	title: '无此用户！',
					// 	icon: "none",
					// 	duration: 1500
					// });
					uni.removeStorageSync("token")
					uni.reLaunch({
						url: "/pages/login/index"
					});
					// setTimeout(() => {
					// 	uni.reLaunch({
					// 		url: "/pages/login/index"
					// 	});
					// }, 2000)
					return
				} else if (res.data.code == 1004) {
					uni.showToast({
						title: '账户被封禁！',
						icon: "none",
						duration: 1500
					});
					uni.removeStorageSync("token")
					// setTimeout(() => {
					// 	uni.reLaunch({
					// 		url: "/pages/login/index"
					// 	});
					// })
					return
				} else if (res.data.code == 1002) {
					uni.showToast({
						title: '验证已过期，请重新登录！',
						icon: "none",
						duration: 1500
					});
					uni.removeStorageSync("token")
					// setTimeout(() => {
					// 	uni.reLaunch({
					// 		url: "/pages/login/index"
					// 	});
					// })
					return
				} else if (res.data.code == 1003) {
					uni.showToast({
						title: '验证已过期，请重新登录！',
						icon: "none",
						duration: 1500
					});
					uni.removeStorageSync("token")
					// setTimeout(() => {
					// 	uni.reLaunch({
					// 		url: "/pages/login/index"
					// 	});
					// })
					return
				} else if (res.data.code == 1005) {
					uni.showToast({
						title: '验证失败，请重新登录！',
						icon: "none",
						duration: 1500
					});
					uni.removeStorageSync("token")
					// setTimeout(() => {
					// 	uni.reLaunch({
					// 		url: "/pages/login/index"
					// 	});
					// })
					return
				} else {
					reject(res);
				}

			})
			.catch((response) => {
				reject(response);
			});
	});
	return promise;
};
export default {
	BASE_URL,
	request,
};